#------------------------------------------------------------------------------
# PostgreSQL configuration file (Managed by Ansible)
#------------------------------------------------------------------------------
#
# This file consists of lines of the form:
#
#   name = value
#
# (The "=" is optional.)  Whitespace may be used.  Comments are introduced with
# "#" anywhere on a line.  The complete list of parameter names and allowed
# values can be found in the PostgreSQL documentation.
#
# The commented-out settings shown in this file represent the default values.
# Re-commenting a setting is NOT sufficient to revert it to the default value;
# you need to reload the server.
#
# This file is read on server startup and when the server receives a SIGHUP
# signal.  If you edit the file on a running system, you have to SIGHUP the
# server for the changes to take effect, run "pg_ctl reload", or execute
# "SELECT pg_reload_conf()".  Some parameters, which are marked below,
# require a server shutdown and restart to take effect.
#
# Any parameter can also be given as a command-line option to the server, e.g.,
# "postgres -c log_connections=on".  Some parameters can be changed at run time
# with the "SET" SQL command.
#
# Memory units:  kB = kilobytes        Time units:  ms  = milliseconds
#                MB = megabytes                     s   = seconds
#                GB = gigabytes                     min = minutes
#                TB = terabytes                     h   = hours
#                                                   d   = days

#------------------------------------------------------------------------------
# Default
#------------------------------------------------------------------------------

# CONNECTIONS AND AUTHENTICATION

listen_addresses = '*'
port = 5432
max_connections = 100

# RESOURCE USAGE (except WAL)

shared_buffers = 128MB
work_mem = 16MB
maintenance_work_mem = 64MB
autovacuum_work_mem = 32MB
dynamic_shared_memory_type = posix
temp_file_limit = 1GB
shared_preload_libraries = 'pg_stat_statements'

# WRITE-AHEAD LOG

wal_level = logical
fsync = on
synchronous_commit = on
full_page_writes = on
checkpoint_timeout = 10min
max_wal_size = 1GB
min_wal_size = 80MB
checkpoint_completion_target = 0.9
checkpoint_warning = 60s
archive_mode = on
archive_command = '/bin/true'
archive_timeout = 600

# REPLICATION

max_wal_senders = 10
max_replication_slots = 10
wal_keep_size = 8GB
hot_standby = on

# REPORTING AND LOGGING

log_destination = 'stderr'
logging_collector = on
log_directory = 'log'
log_filename = 'postgresql-%Y-%m-%d.log'
log_file_mode = 0644
log_truncate_on_rotation = off
log_rotation_age = 1d
log_rotation_size = 0
log_min_duration_statement = '250'
log_checkpoints = on
log_connections = on
log_disconnections = on
log_line_prefix = '%t [%p] %q%h %a %u@%d %e %i '
log_lock_waits = on
log_statement = 'ddl'
log_replication_commands = on
log_temp_files = 0
log_timezone = 'Europe/Moscow'

# STATISTICS

track_io_timing = on

# AUTOVACUUM

autovacuum = on
log_autovacuum_min_duration = 60000
autovacuum_max_workers = 3
autovacuum_vacuum_scale_factor = 0.01
autovacuum_analyze_scale_factor = 0.05

# CLIENT CONNECTION DEFAULTS

datestyle = 'iso, dmy'
timezone = 'Europe/Moscow'
lc_messages = 'en_US.UTF-8'
lc_monetary = 'ru_RU.UTF-8'
lc_numeric = 'ru_RU.UTF-8'
lc_time = 'ru_RU.UTF-8'
default_text_search_config = 'pg_catalog.russian'
idle_in_transaction_session_timeout = '1min'

# CUSTOMIZED OPTIONS

# pg_stat_statements
pg_stat_statements.track = 'top'
pg_stat_statements.max = '10000'
pg_stat_statements.track_utility = 'off'

#------------------------------------------------------------------------------
# Customized config
#------------------------------------------------------------------------------

{% if postgres_config is defined %}
{% for key, value in postgres_config.items() %}
{{ key }} = {{ value }}
{% endfor %}
{% endif %}

#------------------------------------------------------------------------------
# Deploy
#------------------------------------------------------------------------------

data_directory = '{{ postgres_data }}'
hba_file = '{{ postgres_conf }}/pg_hba.conf'
ident_file = '{{ postgres_conf }}/pg_ident.conf'
port = {{ postgres_port }}
log_directory = '{{ postgres_log }}'
